namespace Sage.Http.Authentication
{
    /// <summary>
    /// Bearer Token认证提供者
    /// </summary>
    /// <remarks>
    /// 初始化Bearer Token认证提供者
    /// </remarks>
    /// <param name="token">认证令牌</param>
    /// <param name="scheme">认证方案，默认为"Bearer"</param>
    public class BearerTokenAuthProvider(string token, string scheme = "Bearer") : IAuthenticationProvider
    {
        private readonly string _token = token ?? throw new ArgumentNullException(nameof(token));
        private readonly string _scheme = scheme ?? "Bearer";

        /// <summary>
        /// 获取认证令牌
        /// </summary>
        public string Token => _token;

        /// <summary>
        /// 获取认证方案
        /// </summary>
        public string Scheme => _scheme;

        /// <summary>
        /// 应用Bearer Token认证
        /// </summary>
        /// <param name="request">HTTP请求</param>
        public Task AuthenticateRequest(HttpRequestMessage request)
        {
            ArgumentNullException.ThrowIfNull(request);

            request.Headers.Authorization =
                new System.Net.Http.Headers.AuthenticationHeaderValue(_scheme, _token);

            return Task.CompletedTask;
        }
    }
}
